package com.kylin.module.community.business;

/*
import java.text.SimpleDateFormat;
import java.text.ParseException;
*/
import java.util.Date;
import com.kylin.common.paginate.Paging;
import java.util.List;

import com.kylin.common.exception.BusinessException;

import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;

import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.TransactionDAO;
import com.kylin.module.community.forms.MemberForm;
import com.kylin.module.community.orm.Member;
import com.kylin.module.community.orm.MemberDAO;
import com.kylin.module.community.orm.FriendslistDAO;

//import com.kylin.module.study.orm.VocabularyDAO;
import com.kylin.module.user.orm.User;

import org.apache.log4j.Logger;


public class FriendsBlogic {
	
	private int psgeSize = 15;
	protected static Logger log = Logger.getLogger(FriendsBlogic.class);
 
	/*
	public List getFriends(Long id){
		log.info("get last 10 registers");
		FriendslistDAO fdao = new FriendslistDAO();
		return fdao.findFriends(id);
	}
	*/
	
	/*
	 * 
	 * 
	 */	
	public Paging getFriendsPage(User user){
		FriendslistDAO fdao = new FriendslistDAO();
		Paging page = new Paging();
		page.setTotal(fdao.getTotal(user));
		page.setList(fdao.find(user));
		return page;
	}
	
	public List getFriends(Long userid){
		try{
		User user = (User)BaseHibernateDAO.findById(User.class, userid);
		//User user2 = (User)BaseHibernateDAO.findById(User.class, user2id);
		log.info("get friends list of "+user.getUsername());
		FriendslistDAO fdao = new FriendslistDAO();
		return fdao.list(user);
		}
		catch(BusinessException e){
			return null;
		}
	}

	public Paging show(User user, Integer limitstart, Integer limit){
		Paging page = new Paging();
		FriendslistDAO fdao = new  FriendslistDAO();
		
		page.setTotal(fdao.getTotal(user));
		page.setList(fdao.getList(user, limitstart, limit));
		page.setStart(limitstart);
		page.setLimit(limit);
		
		return page;		
	}	
	
	public List list(Long userid){
		try{
		FriendslistDAO fdao = new FriendslistDAO();
		User user = (User)BaseHibernateDAO.findById(User.class, userid);
		return fdao.list(user);
		}
		catch(Exception e){
			return null;
		}
	}	
	
	
	/*
	 * 
	 * 
	 */
	public void editFriend(Long id){
		try{
		User user = (User)BaseHibernateDAO.findById(User.class, id);
		log.info("get last 10 registers"+user.getUsername());
		FriendslistDAO fdao = new FriendslistDAO();
		//return fdao.findFriends(user);
		}
		catch(BusinessException e){
			//return null;
		}
	}	
	
	/*
	 * 
	 * 
	 */
	public void addFriend(User user, Long user2id){
		try{

		User user2 = (User)BaseHibernateDAO.findById(User.class, user2id);
		if (user2 != null){
			log.info("add friend of "+user.getUsername());
			FriendslistDAO fdao = new FriendslistDAO();
			fdao.add(user, user2);
		}
		//return fdao.findFriends(user);
		}
		catch(org.hibernate.exception.ConstraintViolationException cve){
			log.info("ConstraintViolationException "+ cve.toString());
		}
		catch(BusinessException e){
			//return null;
		}
	}	 
	
}
